-- UserID登录
ALTER PROCEDURE [dbo].[CPP_EfficacyUserID]
  @dwUserID INT,              -- 用户ID
  @strDynamicPass NCHAR(32),  -- 动态密码
  @strClientIP NVARCHAR(15),  -- 连接地址
  @strMachineID NVARCHAR(32), -- 机器标识
  @wKindID SMALLINT,          -- 游戏ID=>RYPlatformDB.dbo.GameKindItem.KindID
  @wServerID SMALLINT,        -- 房间ID
  @nRadius  float,
  @sRadius  float ,
  @szAddress  NCHAR(128),
  @szResult NVARCHAR(255) OUTPUT    -- 输出信息
AS
BEGIN
  -- 属性设置
  SET NOCOUNT ON

  -- 基本信息
  DECLARE @UserID INT = 0
  DECLARE @FaceID SMALLINT
  DECLARE @CustomID INT
  DECLARE @NickName NVARCHAR(31)
  DECLARE @UnderWrite NVARCHAR(63)

  -- 附属信息
  DECLARE @QQ NVARCHAR(16) = N''
  DECLARE @EMail NVARCHAR(16) = N''
  DECLARE @SeatPhone NVARCHAR(32) = N''
  DECLARE @MobilePhone NVARCHAR(16) = N''
  DECLARE @DwellingPlace NVARCHAR(128) = N''
  DECLARE @PostalCode NVARCHAR(8) = N''

  -- 扩展信息
  DECLARE @GameID INT
  DECLARE @GroupID INT = 0
  DECLARE @UserRight INT
  DECLARE @Gender TINYINT
  DECLARE @MasterRight INT
  DECLARE @MasterOrder SMALLINT
  DECLARE @MemberOrder SMALLINT
  DECLARE @GroupName NVARCHAR(31) = ''

  -- 积分变量
  DECLARE @Score BIGINT = 0   -- 身上金币
  DECLARE @Insure BIGINT = 0  -- 保险箱金币
  DECLARE @Grade BIGINT = 0
  DECLARE @WinCount INT = 0
  DECLARE @LostCount INT = 0
  DECLARE @DrawCount INT = 0
  DECLARE @FleeCount INT = 0
  DECLARE @UserMedal INT
  DECLARE @Experience INT
  DECLARE @LoveLiness INT
  DECLARE @IntegralCount BIGINT = 0
  DECLARE @lRoomCard BIGINT = 0
  DECLARE @RegisterTime DATETIME

  -- 辅助变量
  DECLARE @EnjoinLogon INT = 0

  -- 系统暂停
  SELECT @EnjoinLogon = ISNULL(StatusValue, 0) FROM RYAccountsDBLink.RYAccountsDB.dbo.SystemStatusInfo WHERE StatusName=N'EnjoinLogon'
  IF @EnjoinLogon<>0 BEGIN
    SELECT @szResult = StatusString FROM RYAccountsDBLink.RYAccountsDB.dbo.SystemStatusInfo WHERE StatusName=N'EnjoinLogon'
    RETURN 1
  END

  -- 效验地址
  SELECT @EnjoinLogon = ISNULL(EnjoinLogon, 0) FROM ConfineAddress(NOLOCK) WHERE AddrString=@strClientIP AND GETDATE()<EnjoinOverDate
  IF @EnjoinLogon<>0 BEGIN
    SET @szResult = N'抱歉地通知您,系统禁止了您所在的 IP 地址的游戏登录权限,请联系客户服务中心了解详细情况!'
    RETURN 2
  END

  -- 效验机器
  SELECT @EnjoinLogon = ISNULL(EnjoinLogon, 0) FROM ConfineMachine(NOLOCK) WHERE MachineSerial=@strMachineID AND GETDATE()<EnjoinOverDate
  IF @EnjoinLogon<>0 BEGIN
    SET @szResult = N'抱歉地通知您,系统禁止了您的机器的游戏登录权限,请联系客户服务中心了解详细情况!'
    RETURN 3
  END

  -- 查询用户
  DECLARE @Nullity BIT
  DECLARE @StunDown BIT
  DECLARE @DynamicPass AS NCHAR(32) = ''
  DECLARE @MachineID NVARCHAR(32)
  DECLARE @UserUUID NVARCHAR(32)
  DECLARE @MoorMachine AS TINYINT
  DECLARE @SpreaderID INT = 0
  DECLARE @WxImageUrl NVARCHAR(200) = ''

  SELECT
    @UserID = ISNULL(a.UserID, 0), @GameID = ISNULL(GameID, 0), @NickName = NickName,
    @UnderWrite = UnderWrite, @DynamicPass = DynamicPass,
    @FaceID = FaceID, @CustomID = CustomID, @Gender = Gender, @Nullity = Nullity,
    @StunDown = StunDown, @UserMedal = UserMedal, @Experience = Experience,
    @LoveLiness = LoveLiness, @UserRight = UserRight, @MasterRight = MasterRight,
    @MasterOrder = MasterOrder, @MemberOrder = MemberOrder, @MoorMachine = MoorMachine,
    @MachineID = LastLogonMachine, @RegisterTime = RegisterDate, @UserUUID = RegisterMachine,
    @QQ = ISNULL(QQ, ''), @EMail = ISNULL(EMail, ''), @SeatPhone = ISNULL(SeatPhone, ''),
    @MobilePhone = ISNULL(MobilePhone, ''), @DwellingPlace = ISNULL(DwellingPlace, ''),
    @PostalCode = ISNULL(PostalCode, ''), @WxImageUrl = ISNULL(HeadImgUrl, '')
  FROM
    RYAccountsDBLink.RYAccountsDB.dbo.AccountsInfo a
  LEFT JOIN RYAccountsDBLink.RYAccountsDB.dbo.IndividualDatum b ON a.UserID = b.UserID
  WHERE a.UserID = @dwUserID

  -- 查询用户
  IF @UserID = 0 BEGIN
    SET @szResult = N'您的帐号不存在或者密码输入有误,请查证后再次尝试登录!'
    RETURN 4
  END

  -- 帐号禁止
  IF @Nullity<>0 BEGIN
    SET @szResult = N'您的帐号暂时处于冻结状态,请联系客户服务中心了解详细情况!'
    RETURN 5
  END

  -- 帐号关闭
  IF @StunDown<>0 BEGIN
    SET @szResult = N'您的帐号使用了安全关闭功能,必须重新开通后才能继续使用!'
    RETURN 6
  END

  -- 固定机器
  IF @MoorMachine=1 AND @MachineID<>@strMachineID BEGIN
    SET @szResult = N'您的帐号使用固定机器登录功能,您现所使用的机器不是所指定的机器!'
    RETURN 7
  END

  ---- 密码判断
  --IF @DynamicPass<>@strDynamicPass AND @strClientIP<>N'0.0.0.0' BEGIN
  --  SET @szResult = N'抱歉,由于您的帐号在其它地方登录,请重新登录平台后再次尝试!'
  --  RETURN 8
  --END

  -- 固定机器
  IF @MoorMachine=2 BEGIN
    SET @szResult = N'您的帐号成功使用了固定机器登录功能!'
    UPDATE RYAccountsDBLink.RYAccountsDB.dbo.AccountsInfo SET MoorMachine=1, LastLogonMachine=@strMachineID WHERE UserID=@UserID
  END

  -- 小游戏判断
  DECLARE @KindID INT = 0
  SELECT @KindID = ISNULL(KindID, 0) FROM RYPlatformDB.dbo.GameKindItem WHERE Nullity = 0 AND KindID = @wKindID
  IF @KindID = 0 BEGIN
    SET @szResult = N'游戏[' + CAST(@wKindID AS NVARCHAR) + ']未定义'
    RETURN 9
  END

	-- ServerID判断
  DECLARE @ServerID INT = 0
  SELECT @ServerID = ISNULL(ServerID, 0) FROM RYPlatformDB.dbo.GameRoomInfo WHERE Nullity = 0 AND KindID = @wKindID AND ServerID = @wServerID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'房间服务[' + CAST(@wServerID AS NVARCHAR) + ']未定义'
    RETURN 10
  END

  -- 游戏信息
  DECLARE @AllLogonTimes INT = 0
  DECLARE @GameUserRight INT = 0
  DECLARE @GameMasterRight INT = 0
  DECLARE @GameMasterOrder SMALLINT = 0
  DECLARE @InoutIndex BIGINT = 0
  
  SELECT
    @Score = ISNULL(Score, 0), @Insure = ISNULL(InsureScore, 0),
    @WinCount = ISNULL(WinCount, 0), @LostCount = ISNULL(LostCount, 0),
    @DrawCount = ISNULL(DrawCount, 0), @FleeCount = ISNULL(FleeCount, 0),
    @AllLogonTimes = ISNULL(AllLogonTimes, 0), @GameUserRight = ISNULL(UserRight, 0),
    @GameMasterRight = ISNULL(MasterRight, 0), @GameMasterOrder = ISNULL(MasterOrder, 0)
  FROM GameScoreInfo WHERE UserID = @UserID
  
  IF @@ROWCOUNT=0 BEGIN
    INSERT INTO GameScoreInfo (UserID, LastLogonIP, LastLogonMachine, RegisterIP, RegisterMachine)
    VALUES (@UserID, @strClientIP, @strMachineID, @strClientIP, @strMachineID)
  END

  -- 进入记录
  INSERT RecordUserInout (
    UserID, EnterScore, EnterGrade, EnterInsure, EnterUserMedal, EnterLoveliness,
    KindID, ServerID, EnterClientIP, EnterMachine
  ) VALUES (
    @UserID, @Score, @Grade, @Insure, @UserMedal, @Loveliness,
    @wKindID, @wServerID, @strClientIP, @strMachineID
  )

  IF @strClientIP<>N'0.0.0.0' BEGIN -- 查询锁定
    DECLARE @LockKindID INT = 0
    DECLARE @LockServerID INT = 0
    SELECT
      @LockKindID = ISNULL(KindID, 0), @LockServerID = ISNULL(ServerID, 0), @InoutIndex = ISNULL(EnterID, 0)
    FROM GameScoreLocker WHERE UserID = @UserID

    IF @@ROWCOUNT=0 BEGIN
      SET @InoutIndex = SCOPE_IDENTITY()
      INSERT GameScoreLocker (UserID, ServerID, KindID, EnterID, EnterIP, EnterMachine,IsMobile)
      VALUES (@UserID, @wServerID, @wKindID, @InoutIndex, @strClientIP, @strMachineID,0)
    END ELSE BEGIN
      IF @LockKindID=0 BEGIN
        SET @szResult = N'您正在进行保险柜处理过程中,暂时无法进入此游戏房间!'
        RETURN 11
      END ELSE BEGIN
        DECLARE @KindName NVARCHAR(31) = '未知游戏'
        DECLARE @ServerName NVARCHAR(31) = '未知房间'
        SELECT @KindName = KindName FROM RYPlatformDBLink.RYPlatformDB.dbo.GameKindItem WHERE KindID = @LockKindID
        SELECT @ServerName = ServerName FROM RYPlatformDBLink.RYPlatformDB.dbo.GameRoomInfo WHERE ServerID = @LockServerID
        SET @szResult = N'您正在['+@KindName+N']的['+@ServerName+N']游戏中,不能同时再进入此游戏房间!'
        RETURN 12
      END
    END
  END

  -- 权限标志
  SET @UserRight = @UserRight|@GameUserRight
  SET @UserRight = @UserRight&~256
  SET @MasterRight = @MasterRight|@GameMasterRight

  -- 权限等级
  IF @GameMasterOrder>@MasterOrder SET @MasterOrder=@GameMasterOrder

   -- 刷新ip
  UPDATE RYAccountsDB.dbo.AccountsInfo SET LastLogonIP = @strClientIP WHERE UserID = @UserID

  --更新信息
  UPDATE GameScoreInfo SET AllLogonTimes=AllLogonTimes+1, LastLogonDate=GETDATE(),
    LastLogonIP=@strClientIP,LastLogonMachine=@strMachineID WHERE UserID=@UserID

  -- 附属资料
  UPDATE RYAccountsDB.dbo.IndividualDatum SET nRadius = @nRadius, sRadius = @sRadius WHERE UserID = @userid

  -- 登录统计
  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)

  -- 插入记录
  IF @AllLogonTimes>0 BEGIN
    UPDATE SystemStreamInfo SET LogonCount=LogonCount+1 WHERE DateID=@DateID AND KindID=@wKindID AND ServerID=@wServerID
    IF @@ROWCOUNT=0 INSERT SystemStreamInfo (DateID, KindID, ServerID, LogonCount) VALUES (@DateID, @wKindID, @wServerID, 1)
  END ELSE BEGIN
    UPDATE SystemStreamInfo SET RegisterCount=RegisterCount+1 WHERE DateID=@DateID AND KindID=@wKindID AND ServerID=@wServerID
    IF @@ROWCOUNT=0 INSERT SystemStreamInfo (DateID, KindID, ServerID, RegisterCount) VALUES (@DateID, @wKindID, @wServerID, 1)
  END

  -- 房间参数
  SELECT @IntegralCount = ISNULL(IntegralCount, 0) FROM GameScoreAttribute WHERE UserID=@UserID AND ServerID=@wServerID AND KindID = @wKindID

  -- 用户房卡
  SELECT @lRoomCard=ISNULL(RoomCard, 0), @SpreaderID = ISNULL(SpreaderID, 0) FROM RYTreasureDB.dbo.UserRoomCard WHERE UserID=@UserID AND KindID = @wKindID

  DECLARE @m BIGINT
  EXEC RYAccountsDB.dbo.GSP_GP_GetUserDayScore_Control @userid,@m OUTPUT

  DECLARE @GroupIDArray NVARCHAR(512)
  EXEC RYAccountsDB.dbo.CPP_Family_Get2 @UserID,@GroupIDArray OUTPUT

  -- 输出变量
  SELECT
    @UserID AS UserID, @GameID AS GameID, @GroupID AS GroupID, @NickName AS NickName,
    @UnderWrite AS UnderWrite,  @FaceID AS FaceID, @CustomID AS CustomID,
    @Gender AS Gender, @GroupName AS GroupName, @MasterOrder AS MemberOrder,
    @UserRight AS UserRight, @MasterRight AS MasterRight, @MasterOrder AS MasterOrder,
    @MemberOrder AS MemberOrder, @Score AS Score, @m AS Grade, @Insure AS Insure,
    @UserMedal AS Ingot, @WinCount AS WinCount, @LostCount AS LostCount, @DrawCount AS DrawCount,
    @FleeCount AS FleeCount,@Experience AS Experience, @LoveLiness AS LoveLiness,
    @InoutIndex AS InoutIndex, @IntegralCount AS IntegralCount, @QQ AS QQ, 
    @EMail AS EMail, @SeatPhone AS SeatPhone, @MobilePhone AS MobilePhone,
    @DwellingPlace AS DwellingPlace,@PostalCode AS PostalCode, @RegisterTime AS RegisterTime,
    @strClientIP AS ClientIP, @lRoomCard AS RoomCard, @SpreaderID AS SpreaderID,
    @WxImageUrl AS WxImageUrl,@UserUUID AS RegisterMachine, @GroupIDArray AS GroupIDArray

  RETURN 0
END
